package org.jim.server.command.handler;

import cn.hutool.core.util.ObjectUtil;
import org.jim.core.ImChannelContext;
import org.jim.core.ImPacket;
import org.jim.core.ImStatus;
import org.jim.core.exception.ImException;
import org.jim.core.packets.CloseReqBody;
import org.jim.core.packets.Command;
import org.jim.core.packets.RespBody;
import org.jim.core.utils.JsonKit;
import org.jim.server.JimServerAPI;
import org.jim.server.command.AbstractCmdHandler;
import org.jim.server.processor.login.LoginCmdProcessor;
import org.jim.server.processor.login.LogoutCmdProcessor;
import org.jim.server.protocol.ProtocolManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Resource;

/**
 * 版本: [1.0]
 * 功能说明: 关闭请求cmd命令处理器
 * @author : WChao 创建时间: 2017年9月21日 下午3:33:23
 */
public class CloseReqHandler extends AbstractCmdHandler {

	private static final Logger log = LoggerFactory.getLogger(CloseReqHandler.class);

	@Override
	public ImPacket handler(ImPacket packet, ImChannelContext imChannelContext) throws ImException {

		log.info("关闭请求 cmd 处理器");

		CloseReqBody closeReqBody;
		try{
			closeReqBody = JsonKit.toBean(packet.getBody(),CloseReqBody.class);

			log.info("关闭请求请求参数为：{}",closeReqBody);

		}catch (Exception e) {
			//关闭请求消息格式不正确
			return ProtocolManager.Converter.respPacket(new RespBody(Command.COMMAND_CLOSE_REQ, ImStatus.C10020), imChannelContext);
		}
		JimServerAPI.bSend(imChannelContext, ProtocolManager.Converter.respPacket(new RespBody(Command.COMMAND_CLOSE_REQ, ImStatus.C10021), imChannelContext));
		if(closeReqBody == null || closeReqBody.getUserId() == null){
			JimServerAPI.remove(imChannelContext, "收到关闭请求");
		}else{
			String userId = closeReqBody.getUserId();
			JimServerAPI.remove(userId, "收到关闭请求!");
		}

		// 获取退出命令处理器实例
		LogoutCmdProcessor loginProcessor = this.getSingleProcessor(LogoutCmdProcessor.class);

		/**
		 * 兼容因网络异常导致连接断开，无法获取到用户信息，直接执行退出登录处理
		 */
		if(!ObjectUtil.isNull(closeReqBody)){
			loginProcessor.doLogout(closeReqBody.getUserId(),imChannelContext);
		}

		return null;
	}

	@Override
	public Command command() {
		return Command.COMMAND_CLOSE_REQ;
	}
}
